package com.ld.admin.service.system.filelistener;

import java.io.File;

import org.springframework.stereotype.Service;

import com.ld.admin.model.system.NoticeActionModel;
import com.ld.admin.model.system.filelistener.SysFilelistenerModel;
import com.ld.common.service.ProjectBasicService;
import com.ld.common.util.filelistener.commonio.FileChangedListenerManager;
import com.ld.common.util.filelistener.commonio.FileChangedListenerModel;
import com.ld.common.util.filelistener.commonio.plugin.ProjectFileListenerPlugin;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

/**
 * 
 * 文件监控器服务类，@Service注解用于自动注入
 * 
 * @ClassName SysTimedtaskService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年01月18日 14:29:47
 *
 */
@Service
public class SysFileLinstenerService extends ProjectBasicService {

    @Override
    public <T> Result addOrUpdate(T modelBean, boolean update) {
        SysFilelistenerModel model = (SysFilelistenerModel) modelBean;
        String path = model.getPath(); // 监控路径
        if (path != null) {
            File file = new File(path);
            if (!file.exists()) {
                return Web.Response.error("路径不存在！");
            }
        } else {
            return Web.Response.error("路径不能为空！");
        }
        return super.addOrUpdate(modelBean, update);
    }

    /**
     * 启动结束前的操作
     * 
     * @Title extendBeforeStartOrStop
     * @author 吕凯
     * @date 2019年3月2日 上午9:13:04
     * @param classOfT
     * @param key
     * @param keyValue
     * @param isStart
     * @return
     * @see com.ld.shieldsb.common.web.BasicService#extendBeforeStartOrStop(java.lang.Class, java.lang.String, java.lang.Object, int)
     */
    @Override
    public <T> Result extendBeforeStartOrStop(Class<T> classOfT, String key, Object keyValue, int isStart) {
        Result result = new Result();
        SysFilelistenerModel model = getBaseDAO().findById(SysFilelistenerModel.class, key, keyValue);
        if (model != null) {
            FileChangedListenerModel listnerModel = FileChangedListenerManager.get(model.getKeyCode());
            if (isStart == 1) { // 启动时判断路径是否存在，关闭不判断
                File file = new File(model.getPath());
                if (!file.exists()) {
                    return Web.Response.error("路径不存在！");
                }
            }
            QueryModel queryModel = new QueryModel();
            queryModel.addEq("pkey", model.getId());
            queryModel.addEq("keyCode", "sys_filelistener");
            NoticeActionModel noticActionModel = getBaseDAO().getOne(NoticeActionModel.class, queryModel); // 通知配置
            if (listnerModel == null) { // 不存在则先注 册
                Result registerResult = ProjectFileListenerPlugin.register(model, noticActionModel);
                if (!registerResult.getSuccess()) {
                    return registerResult;
                }
            } else {
                ProjectFileListenerPlugin.update(model, noticActionModel); // 已经注册的，则更新一下
            }
            try {
                if (isStart == 1) { // 启动
                    result = FileChangedListenerManager.start(model.getKeyCode());
                } else {
                    result = FileChangedListenerManager.stop(model.getKeyCode());
                }
                result.setSuccess(true);
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return result;
    }

}
